package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.dao.ResourceDao;
import com.example.demo.dao.UserDao;
import com.example.demo.pojo.HistoryRecord;
import com.example.demo.pojo.Resource;
import com.example.demo.pojo.User;

@Service("resourceService")
@Transactional
public class ResourceServiceImpl implements ResourceService {
	@Autowired
	private ResourceDao resourceDao;

	@Override
	public List<Resource> findMainResource(Resource resource) {
		// TODO Auto-generated method stub
		System.out.println("前端主界面传来的数据："+resource);
		String resourceCategory;
		resourceCategory=resource.getResourceCategory();
		System.out.println(resourceCategory);
		List<Resource>listResource=resourceDao.findMainResource(resourceCategory);
		return listResource;
	}

	@Override
	public int saveUploadFileInfo(Resource resource) {
		// TODO Auto-generated method stub
		System.out.println("保存上传文件...");
		int row;
		int result;
		row=resourceDao.saveUploadFileInfo(resource);
		if(row>0) {
			//�ɹ�������ע���û�������1
			result=1;
		}else {
			//ʧ�ܷ���0
			result=0;
		}
		return result;
	}

	@Override
	public List<Resource> findMainResourceToAndroid() {
		// TODO Auto-generated method stub
		List<Resource> resourcelist=new ArrayList<>();
		resourcelist=resourceDao.findMainResourceToAndroid();
		System.out.println(resourcelist);
		return resourcelist;
	}

	@Override
	public List<Resource> findUserHistoryResource(List<Resource> resource) {
		// TODO Auto-generated method stub
		int returnMSG=0;
		List<Resource> msgFromDao=new ArrayList();
		System.out.println("�ӿ����������������ǣ�"+resource);
		List<Resource> resourcelist=new ArrayList();
		try {
			for(int i=0;i<resource.size();i++) {
//			msgFromDao=resourceDao.findUserHistoryResource(resource.get(i));
			msgFromDao.addAll(resourceDao.findUserHistoryResource(resource.get(i)));
//			resourcelist.add(msgFromDao);
			}
			System.out.println("���ݿ�ķ��ؽ���ǣ�"+msgFromDao);
			if(msgFromDao!=null) {
				//�ɹ�����
				returnMSG=1;
			}else {
				//ʧ�ܲ���
				returnMSG=2;
			}
		} catch (Exception e) {
			//�쳣��������BUG��
			System.out.println("δ�ܲ��ҵ����û�");
			returnMSG=0;
			// TODO: handle exception
		}
		return msgFromDao;
	}

	/*
	 * ��������������list�������
	 * */
	@Override
	public List<Resource> giveSearchResultList(Resource resource) {
		// TODO Auto-generated method stub
		int returnMSG=0;
		List<Resource> msgFromDao=new ArrayList();
		System.out.println("�ӿ����������������ǣ�"+resource);
		List<Resource> resourcelist=new ArrayList();
		try {
			msgFromDao.addAll(resourceDao.giveSearchResultList(resource.getResourceTitle()));
			System.out.println("���ݿ�ķ��ؽ���ǣ�"+msgFromDao);
			if(msgFromDao!=null) {
				//�ɹ�����
				returnMSG=1;
			}else {
				//ʧ�ܲ���
				returnMSG=2;
			}
		} catch (Exception e) {
			//�쳣��������BUG��
			System.out.println("δ�ܲ��ҵ����û�");
			returnMSG=0;
			// TODO: handle exception
		}
		return msgFromDao;
	}

	/*
	* 返回resource表中有多少数据
	* */
	@Override
	public int countResource(Resource resource){
		int msgFromDao=0;
		msgFromDao=resourceDao.countResource(resource);
		System.out.println("resource表中含有"+msgFromDao+"条数据");
		return msgFromDao;
	}

	/*
	 * 主页面推荐功能
	 * */
	@Override
	public List<Resource> resourceForRecommend(int start){
		List<Resource> msgFromDao=new ArrayList<>();
		msgFromDao=resourceDao.resourceForRecommend(start);
		System.out.print(msgFromDao);
		return msgFromDao;
	}

	//！！！！！！！！！！！后台功能！！！！！！！！！！！！
	@Override
	public int backResourceUpdataInfo(Resource resource) {
		// TODO Auto-generated method stub
		int returnMSG=0;
		int msgFromDao;
		System.out.println("�ӿ����������������ǣ�"+resource);
		Resource userInfo=new Resource();
		try {
			msgFromDao=resourceDao.backResourceUpdataInfo(resource.getResourceId(),resource.getResourceTitle(),resource.getResourceUrl(),resource.getResourceCategory(),resource.getResourceCover(),resource.getTag1Name(),resource.getTag2Name(),resource.getTag3Name(),resource.getTag4Name(),resource.getTag5Name());
			System.out.println("���ݿ�ķ��ؽ���ǣ�"+msgFromDao);
			if(msgFromDao>0) {
				returnMSG=1;
			}else {
				returnMSG=2;
			}
		} catch (Exception e) {
			System.out.println("δ�ܲ��ҵ����û�");
			returnMSG=0;
			// TODO: handle exception
		}
		return returnMSG;
	}

}
